﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TDBaseDataLibrary;
namespace TDFlowLibrary
{
    public enum mainmenu_command
    {
        mainmenu_normal = 0,
        mainmenu_level = 1,
        mainmenu_editmap = 2,
        mainmenu_exit = 3,
    }
    public class MenuItem
    {
        public String strName;
        public String strTex;
        public Texture2D DImage;
        public Texture2D NImage;
        public Texture2D DisImage;
        public Texture2D FImage;
        public Rectangle rtHit;
    }
    public class MainMenu : TDBaseScene
    {
        Texture2D backgroup;
        Texture2D m_Cursor;
        Texture2D m_SelCursor;
        Rectangle rtbtDes;
        List<MenuItem> m_ButtonItem;
        public mainmenu_command command;
        String m_strBackGround;
        String m_strCursor;
        String m_strSelIcon;
        int m_nCurSelItem;
        
        bool m_boverlayer;

        bool m_bLbuttonDown;
        bool bOnStart = false;
        int btnindex = -1;
        public MainMenu(Game game):base(game)
        {
            m_boverlayer = false;
            m_ButtonItem = new List<MenuItem>();
            m_strCursor = "target1_";
            m_bLbuttonDown = false;
            //vstartpos.X = 48;
            //vstartpos.Y = 197;
            //vstartService.X = 179;
            //vstartService.Y = 329;
        }
        public void SetOverlayerMode(bool bOverlayer)
        {
            m_boverlayer = bOverlayer;
        }
        public void SetBackGround(String strBackGround)
        {
            m_strBackGround = strBackGround;
        }
        public void SetSelIcon(String strSelIcon)
        {
            m_strSelIcon = strSelIcon;
        }
        public void AddMenuItem(string szName,string strTex,Rectangle rtHit)
        {
            MenuItem Item = new MenuItem();
            Item.strName = szName;
            Item.strTex = strTex;
            Item.rtHit = rtHit;
            m_ButtonItem.Add(Item);
        }
        public void AddMenuItem(string szName, Texture2D Tex, Rectangle rtHit)
        {
            MenuItem Item = new MenuItem();
            Item.strName = szName;
            Item.NImage = Tex;
            Item.rtHit = rtHit;
            m_ButtonItem.Add(Item);
        }

        public override void  LoadContent(ContentManager Content)
        {
            for (int i = 0; i < m_ButtonItem.Count; i++)
            {
                if(m_ButtonItem[i].strTex != null)
                    m_ButtonItem[i].NImage = Content.Load<Texture2D>(m_ButtonItem[i].strTex);
            }
            if(m_strBackGround != null)
                backgroup = Content.Load<Texture2D>(m_strBackGround);
            if(m_strCursor != null)
            m_Cursor = Content.Load<Texture2D>(m_strCursor);
            if(m_strSelIcon != null)
                m_SelCursor = Content.Load<Texture2D>(m_strSelIcon);
            
            //backgroup = Content.Load<Texture2D>("mainmenu");
            //backgroup_back = Content.Load<Texture2D>("mainglow");
            //BigButton = Content.Load<Texture2D>("mainbigbtn");
            //SmallButton = Content.Load<Texture2D>("mainsmallbtn");
            //hotRect = new Rectangle[6];
            //hotRect[0] = new Rectangle( 48, 197, 110, 110);
            //hotRect[1] = new Rectangle(175, 325, 110, 110);
            //hotRect[2] = new Rectangle(475, 195, 120, 40);
            //hotRect[3] = new Rectangle(445, 265, 120, 40);
            //hotRect[4] = new Rectangle(445, 335, 120, 40);
            //hotRect[5] = new Rectangle(475, 405, 120, 40);
            //command = mainmenu_command.mainmenu_normal;
        }
        public override int Update(GraphicsDeviceManager graphics, GameTime gameTime)
        {
            MouseState mouseState = Mouse.GetState();
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                if (!m_bLbuttonDown)
                    m_bLbuttonDown = true;
            }
            else
            {
                if (m_bLbuttonDown)
                {
                    m_nCurSelItem = btnindex;
                    m_bLbuttonDown = false;
                    return m_nCurSelItem;
                }
            }


            if (m_bLbuttonDown)
            {
                bOnStart = false;
                for (int i = 0; i < m_ButtonItem.Count; i++)
                {
                    if (mouseState.X > m_ButtonItem[i].rtHit.Left && mouseState.X < m_ButtonItem[i].rtHit.Right &&
                        mouseState.Y > m_ButtonItem[i].rtHit.Top && mouseState.Y < m_ButtonItem[i].rtHit.Bottom)
                    {
                        bOnStart = true;
                        btnindex = i;
                        break;
                    }
                }
                if (bOnStart == false)
                {
                    btnindex = -1;
                }
            }

            //for (int i = 0; i < 6; i++)
            //{
            //    rtbtDes = new Rectangle(hotRect[i].Left * graphics.PreferredBackBufferWidth / backgroup.Width,
            //        hotRect[i].Top * graphics.PreferredBackBufferHeight / backgroup.Height,
            //        hotRect[i].Width * graphics.PreferredBackBufferWidth / backgroup.Width,
            //        hotRect[i].Height * graphics.PreferredBackBufferHeight / backgroup.Height);
            //    if (mouseState.X > rtbtDes.Left && mouseState.X < rtbtDes.Right &&
            //        mouseState.Y > rtbtDes.Top && mouseState.Y < rtbtDes.Bottom)
            //    {
            //        bOnStart = true;
            //        btnindex = i;
            //        break;
            //    }
            //}
            //if (bOnStart)
            //{
                
            //    if (mouseState.LeftButton == ButtonState.Pressed)
            //    {
            //        m_nCurSelItem = btnindex;
            //        if (btnindex == 0)
            //        {
            //            command = mainmenu_command.mainmenu_level;
            //        }
            //        else if (btnindex == 1)
            //        {
            //            command = mainmenu_command.mainmenu_editmap;
            //        }
            //        return btnindex;
                    
            //    }
            //}
            return -2;
        }
        public override void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch, int xOffset, int yOffset, float fScale)
        {
            Rectangle rtDes = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
//            GraphicsDevice.RenderState
            if (m_boverlayer != true && backgroup != null)
            {
                spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
                spriteBatch.Draw(backgroup, rtDes, Color.White);
                spriteBatch.End();
            }

            if(m_boverlayer != true)
                spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.Additive);
            //spriteBatch.Draw(backgroup_back, rtDes, Color.White);
            for (int i = 0; i < m_ButtonItem.Count; i++)
            {
                spriteBatch.Draw(m_ButtonItem[i].NImage,m_ButtonItem[i].rtHit,Color.White);
            }
            if (m_SelCursor != null && m_nCurSelItem >= 0)
            {
                spriteBatch.Draw(m_SelCursor, m_ButtonItem[m_nCurSelItem].rtHit, Color.White);
            }
            if (m_boverlayer == false && m_Cursor != null)
            {
                Rectangle rtHit = new Rectangle();
                MouseState mstate = Mouse.GetState();
                rtHit.X = mstate.X - m_Cursor.Width / 2;
                rtHit.Y = mstate.Y - m_Cursor.Height / 2;
                rtHit.Width = m_Cursor.Width;
                rtHit.Height = m_Cursor.Height;

                spriteBatch.Draw(m_Cursor, rtHit, Color.White);
            }
            
            //if (bOnStart)
            //{
            //    if(btnindex < 2)
            //        spriteBatch.Draw(BigButton, rtbtDes, Color.White);
            //    else
            //        spriteBatch.Draw(SmallButton, rtbtDes, Color.White);
            //}
            //for (int i = 0; i < 6; i++)
            //{
            //    spriteBatch.Draw(BigButton, hotRect[i], Color.White);
            //    //spriteBatch.Draw(backgroup_back, rtDes, Color.White);
                
            //}
            if(m_boverlayer != true)
                spriteBatch.End();
            
        }
    }
}
